Orientation Sensor (LSensor)
The Orientation Sensor (Windows.Devices.Sensors.OrientationSensor) is used to determine the orientation of the device in 3D space. It provides data about the device's rotation around the X, Y, and Z axes.
- Namespace:
Windows.Devices.Sensors- Assembly:
- Windows.Foundation.UniversalApiContract
- Introduced:
- Windows 8
Overview
The Orientation Sensor is a composite sensor that typically combines data from the accelerometer, gyroscope, and magnetometer to provide a more accurate and stable reading of the device's orientation. This sensor is crucial for applications that require precise knowledge of the device's tilt, roll, and yaw, such as augmented reality, gaming, and navigation.
Properties
Properties
| Name | Type | Description |
|---|---|---|
Default |
OrientationSensor |
Gets the default orientation sensor. |
MinimumReportInterval |
UInt32 |
Gets the minimum report interval supported by the sensor. |
ReportInterval |
UInt32 |
Sets and gets the desired report interval in milliseconds. |
Methods
Methods
| Name | Description |
|---|---|
GetCurrentReading() |
Retrieves the current orientation reading. |
OnReadingChanged(TypedEventHandler<OrientationSensor, OrientationSensorReadingChangedEventArgs> handler) |
Registers a handler for the ReadingChanged event. |
RemoveReadingChanged(TypedEventHandler<OrientationSensor, OrientationSensorReadingChangedEventArgs> handler) |
Removes a handler for the ReadingChanged event. |
Events
Events
| Name | Description |
|---|---|
ReadingChanged |
Occurs when new orientation data is available. |
Reading Data
The OrientationSensorReading object provides the following properties:
Quaternion: Represents the orientation as a quaternion.RotationMatrix: Represents the orientation as a 3x3 rotation matrix.YawPitchRollAccuracy: Indicates the accuracy of the yaw, pitch, and roll values.Timestamp: The time at which the sensor reading was taken.
Usage Example
Here's a C# example of how to use the Orientation Sensor:
using Windows.Devices.Sensors;
using Windows.UI.Core;
using System;
// ...
public sealed partial class MainPage : Page
{
private OrientationSensor _orientationSensor;
private System.Text.StringBuilder _sb;
public MainPage()
{
this.InitializeComponent();
InitializeOrientationSensor();
}
private void InitializeOrientationSensor()
{
_orientationSensor = OrientationSensor.GetDefault();
_sb = new System.Text.StringBuilder();
if (_orientationSensor != null)
{
// Set the desired report interval
uint defaultReportInterval = _orientationSensor.MinimumReportInterval;
uint fastReportInterval = Math.Max(defaultReportInterval, 16); // Aim for ~60 FPS
_orientationSensor.ReportInterval = fastReportInterval;
// Subscribe to the ReadingChanged event
_orientationSensor.ReadingChanged += OrientationSensor_ReadingChanged;
}
else
{
_sb.AppendLine("Orientation sensor not available.");
StatusTextBlock.Text = _sb.ToString();
}
}
private async void OrientationSensor_ReadingChanged(OrientationSensor sender, OrientationSensorReadingChangedEventArgs args)
{
// Update UI on the UI thread
await Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
var reading = args.Reading;
_sb.Clear();
_sb.AppendLine($"Timestamp: {reading.Timestamp}");
if (reading.Quaternion != null)
{
_sb.AppendLine($"Quaternion: X={reading.Quaternion.X:F3}, Y={reading.Quaternion.Y:F3}, Z={reading.Quaternion.Z:F3}, W={reading.Quaternion.W:F3}");
}
if (reading.RotationMatrix != null)
{
_sb.AppendLine($"Rotation Matrix: ");
_sb.AppendLine($" M11={reading.RotationMatrix.M11:F3}, M12={reading.RotationMatrix.M12:F3}, M13={reading.RotationMatrix.M13:F3}");
_sb.AppendLine($" M21={reading.RotationMatrix.M21:F3}, M22={reading.RotationMatrix.M22:F3}, M23={reading.RotationMatrix.M23:F3}");
_sb.AppendLine($" M31={reading.RotationMatrix.M31:F3}, M32={reading.RotationMatrix.M32:F3}, M33={reading.RotationMatrix.M33:F3}");
}
if (reading.YawPitchRollAccuracy != MagnetometerAccuracy.Unknown)
{
_sb.AppendLine($"Accuracy: {reading.YawPitchRollAccuracy}");
}
StatusTextBlock.Text = _sb.ToString();
});
}
// Remember to unsubscribe when the page is no longer needed
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (_orientationSensor != null)
{
_orientationSensor.ReadingChanged -= OrientationSensor_ReadingChanged;
}
base.OnNavigatedFrom(e);
}
}